package org.dylan.bdf.system.model;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 日志的类型
 * 
 * @author dylan
 * 
 */
public class LogType {
	private String key;
	private String name;
	
	private LogType(){}

	public LogType(String key, String name) {
		if (key == null || name == null) {
			throw new IllegalArgumentException(
					"parameter 'key' and 'name' can't be null.");
		}
		this.key = key;
		this.name = name;
	}
	/**
	 * 内置的几种日志类型
	 * @author dylan
	 *
	 */
	public static enum Type {
		login("登录"), logout("注销"), userManage("用户管理"), roleManage("角色管理"), 
		resourceManage("资源管理");
		private Type(String name) {
			this.name = name;
		}
		private String name;
	}
	
	static{
		//添加内置的资源类型
		for(Type type : Type.values()){
			addLogType(type.name(), type.name);
		}
	}

	private static Map<String, LogType> types;

	/**
	 * 通过日志类型的key获取到已经注册的日志类型
	 * 
	 * @param key
	 * @return
	 */
	public static LogType type(String key) {
		return types.get(key);
	}
	
	/**
	 * 获取所有已经注册的日志类型
	 * 
	 */
	public static Collection<LogType> types() {
		return types.values();
	}

	/**
	 * 增加新的日志类型
	 * 
	 * @param logType
	 */
	public static void addLogType(LogType logType) {
		if (logType == null || logType.key == null) {
			throw new IllegalArgumentException(
					"parameter 'logType' can't be null.");
		}
		if(types == null){
			types = Collections
					.synchronizedMap(new HashMap<String, LogType>());
		}
		types.put(logType.key, logType);
	}

	/**
	 * 增加新的日志类型
	 * 
	 * @param logType
	 */
	public static void addLogType(String key, String name) {
		addLogType(new LogType(key, name));
	}

	/**
	 * 增加新的日志类型
	 * 
	 * @param logType
	 */
	public static void addLogTypes(HashMap<String, String> types) {
		if (types == null) {
			throw new IllegalArgumentException(
					"parameter 'types' can not be null.");
		}
		for (Entry<String, String> entry : types.entrySet()) {
			addLogType(new LogType(entry.getKey(), entry.getValue()));
		}
	}

	/**
	 * 增加新的日志类型
	 * 
	 * @param logType
	 */
	public static void setLogTypes(HashMap<String, String> types) {
		if (types == null) {
			throw new IllegalArgumentException(
					"parameter 'types' can not be null.");
		}
		for (Entry<String, String> entry : types.entrySet()) {
			addLogType(new LogType(entry.getKey(), entry.getValue()));
		}
	}

	public String key() {
		return this.key;
	}

	public String name() {
		return this.name;
	}
}
